package com.baidumap.administrator.happystudy.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.ab.dialog.loadingDialog.LoadingDialogUtil;
import com.ab.http.OnMessageResponse;
import com.ab.util.ListUtils;
import com.ab.util.ToastUtil;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.adapter.FragmentOrderTuiKuanShouHou1Adapter;
import com.baidumap.administrator.happystudy.api.Api;
import com.baidumap.administrator.happystudy.base.AppBaseActivity;
import com.baidumap.administrator.happystudy.base.Constant;
import com.baidumap.administrator.happystudy.model.OrderList;
import com.baidumap.administrator.happystudy.view.SwipyRefreshLayout;
import com.socks.library.KLog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;

/**
 * 描述：
 * 作者：xiaoxing on 17/5/2 10:02
 * 邮箱：2235445233@qq.com
 */
public class TuiKuanShouHouList extends AppBaseActivity implements OnMessageResponse {
    private static int currentPage = 1;
    private FragmentOrderTuiKuanShouHou1Adapter mAdapter;
    private ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    @Bind(R.id.main_SwipyRefreshLayout)
    SwipyRefreshLayout mSwipyRefreshLayout;
    ArrayList<HashMap<String, Object>> result = new ArrayList<>();
    @Bind(R.id.list)
    ListView mListView;
    private String mStatus;


    @Bind(R.id.layout_order_empty)
    LinearLayout layout_order_empty;
    @Bind(R.id.tv_tip)
    TextView tv_tip;

    @Override
    public void doBusiness(Context mContext) {
        super.doBusiness(mContext);
        setHeaderBack();
        setHeaderTitle("售后服务列表");
    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_order_list_yi_fu_kuan;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        // 设置分页第一页的索引 默认为0
        mSwipyRefreshLayout.setFirstIndex(currentPage);
        // 监听事件
        mSwipyRefreshLayout.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(int index) {
                currentPage = 1;
                result.clear();
                list.clear();
                mAdapter.notifyDataSetChanged();
                Api.orderLists(TuiKuanShouHouList.this, sHelper.getString(Constant.TOKEN), sHelper.getString(Constant.UID), mStatus);
                mSwipyRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onLoad(int index) {
                mSwipyRefreshLayout.setRefreshing(false);
            }
        });
        // LoadImage初始化
        currentPage = 1;
        mAdapter = new FragmentOrderTuiKuanShouHou1Adapter(this, sHelper);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


//                Map<String, Object> map = (Map<String, Object>) parent.getItemAtPosition(position);
                Intent intent = new Intent(TuiKuanShouHouList.this, OrderDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("order_id", String.valueOf(list.get(position).get("order_id")));
                intent.putExtras(bundle);
                startActivityForResult(intent, 200);
            }
        });
        tv_tip.setText("目前尚没有服务");
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 200) {
            result.clear();
            list.clear();
            mAdapter.notifyDataSetChanged();
            Api.orderLists(TuiKuanShouHouList.this, sHelper.getString(Constant.TOKEN), sHelper.getString(Constant.UID), mStatus);
        }
    }

    @Override
    public void initData() {
        super.initData();
        mStatus = "4";

        LoadingDialogUtil.showGifdialog(getSupportFragmentManager());
        Api.orderLists(this, sHelper.getString(Constant.TOKEN), sHelper.getString(Constant.UID), mStatus);
    }

    @Override
    public void onMessageResponse(String url, JSONObject jo) throws JSONException {
        if (url.equals(Constant.ORDER_GET_LISTS) && jo != null) {

            try {
                OrderList orderList = new OrderList(jo.toString());

                if (orderList != null) {

                    mSwipyRefreshLayout.setVisibility(View.VISIBLE);
                    layout_order_empty.setVisibility(View.GONE);
                    if (orderList.getCode().equals("200")) {

                        List<OrderList.DataBean> dataBeanList = orderList.getData();

                        if (dataBeanList != null) {


                            HashMap<String, Object> map = null;

                            for (int i = 0; i < dataBeanList.size(); i++) {

                                map = new HashMap<>();
                                map.put("shop_id", dataBeanList.get(i).getShop_id());
                                map.put("shop_name", dataBeanList.get(i).getShop_name());
                                map.put("order_no", dataBeanList.get(i).getOrder_no());
                                map.put("status", mStatus);

                                List<OrderList.DataBean.GoodsBean> goodsBeanList = dataBeanList.get(i).getGoods();

                                if (goodsBeanList != null) {
                                    for (int i1 = 0; i1 < goodsBeanList.size(); i1++) {
                                        map.put("order_id", goodsBeanList.get(i1).getOrder_id());
                                        map.put("product_name", goodsBeanList.get(i1).getProduct_name());
                                        map.put("product_img", goodsBeanList.get(i1).getProduct_image());
                                        map.put("product_id", goodsBeanList.get(i1).getProduct_id());
                                        map.put("product_num", goodsBeanList.get(i1).getProduct_num() == null ? "0" : goodsBeanList.get(i1).getProduct_num());
                                        map.put("product_price", goodsBeanList.get(i1).getSale_price());
                                        map.put("distance", goodsBeanList.get(i1).getState() == null ? "0" : goodsBeanList.get(i1).getState());
                                        map.put("order_id", goodsBeanList.get(i1).getOrder_id());
                                        map.put("shop_rating", "0");

                                    }

                                    result.add(map);
                                }
                            }

                            list.addAll(result);//

                            ListUtils.removeDuplicate(list);

                            currentPageAdd(list);
                            mAdapter.bindData(list);

                            if (currentPage == 1) {

                                mListView.setAdapter(mAdapter);
                            }
                            mAdapter.notifyDataSetChanged();
                        }
                    } else {
                        ToastUtil.showMessage(this, orderList.getMsg());
                    }

                } else {
                    mSwipyRefreshLayout.setVisibility(View.GONE);
                    layout_order_empty.setVisibility(View.VISIBLE);
                }
            } catch (Exception e) {
                e.printStackTrace();
                mSwipyRefreshLayout.setVisibility(View.GONE);
                layout_order_empty.setVisibility(View.VISIBLE);
            }
        }
    }

    protected void currentPageAdd(ArrayList mListView) {
        KLog.e("========" + currentPage);
        if (mListView.size() % 20 == 0 && mListView.size() > 0) {
            currentPage++;
        }
    }

    @Override
    public void onMessageResponse(String url, JSONArray jo) throws JSONException {

    }

    @Override
    public void onMessageResponse(String url, String str) throws Exception {

    }
}
